﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class admin_cms_default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                  Load_user();
                // load  checked với opiton

                  loadgorup();
                
            }

         
        }

    }
    protected void Load_user()
    {
        DataTable dtuser = new DataTable();

        dtuser = DataAccess.ExeSQLToDataTable("select * from admin_alluser");

        if (dtuser.Rows.Count > 0)
        {
            GridView1.DataSource = dtuser;
            GridView1.DataBind();
        }
    }
 

    protected void grv_yahoo_click(object sender, CommandEventArgs e)
    {

        string id = e.CommandArgument.ToString();
        string CheckName = e.CommandName.ToString();
        if (CheckName == "X")
        {
            DataAccess.ExeNonSQL("delete  from admin_alluser where id='" + id + "'");
            Load_user();
        }
        else
        {
            DataTable dtgrv_user = new DataTable();
            dtgrv_user = DataAccess.ExeSQLToDataTable("select * from admin_alluser where id='"+id+"'");
            if (dtgrv_user.Rows.Count > 0)
            {
                txtuser.Text = dtgrv_user.Rows[0][1].ToString();
                txtmk.Text = dtgrv_user.Rows[0][2].ToString();
                txtname.Text = dtgrv_user.Rows[0][3].ToString();
                
                Session.Add("id", id);
            }
        }
    }
    protected void User_Insert(object sender, EventArgs e)
    {
        // xử lý nhóm người dùng và quyền
        string nhomnguoidung = "";
        //foreach (ListItem item1 in RadioButtonList1.Items)
        //{

        //    if (item1.Selected)
        //    {
        //        // Response.Write("Bạn đã chọn quyền: " + item1.Value + "<br />");
        //        nhomnguoidung = item1.Value;
        //    }
        //}
        nhomnguoidung = RadioButtonList1.SelectedItem.Value.ToString();
        //foreach (ListItem item in CheckBoxList1.Items)
        //{
            
        //    if (item.Selected)
        //    {
        //        Response.Write("Bạn đã chọn modul: " + nhomnguoidung + "<br />");
        //        Response.Write("Bạn đã chọn modul: " + item.Value + "<br />");
        //        DataAccess.ExeNonSQL("Insert into admin_roles(Idmodule,idgroup) values('" + item.Value + "','" + nhomnguoidung + "')");
        //    }
        //}


        //
        string filename = fileupload.FileName;
        if (filename!="")
        {
            fileupload.SaveAs(Server.MapPath("style/images/" + filename));
             string user = txtuser.Text;
             string pass = txtmk.Text;
             string name = txtname.Text;
             //DataAccess.ExeNonStored("Yahoo_Insert","@Name",Name,"@Nick", Nikc, "@Status", Status, "@Sort", Sort);
             DataAccess.ExeNonSQL("insert into admin_alluser(username,password,fullname,idgroup,picture) values('" + user + "','" + pass + "',N'" + name + "','" + nhomnguoidung + "','"+filename+"')");
             Load_user();
        }
       

    }
    protected void UserUpdate(object sender, EventArgs e)
    {
        string filename = fileupload.FileName; string user = txtuser.Text;
        string pass = txtmk.Text;
        
        string name = txtname.Text;
        string id = Session["id"].ToString();
        if (filename != "")
        {
            fileupload.SaveAs(Server.MapPath("style/images/" + filename));
          
            //DataAccess.ExeNonStored("Yahoo_Insert","@Name",Name,"@Nick", Nikc, "@Status", Status, "@Sort", Sort);
            DataAccess.ExeNonSQL("update admin_alluser set username=N'"+user+"',password='"+pass+"', fullname=N'"+name+"',picture='"+filename+"' where id='"+id+"'");
            Load_user();
        }
        else
        {
            DataAccess.ExeNonSQL("update admin_alluser set username=N'" + user + "',password='" + pass + "', fullname=N'" + name + "',picture='" + filename + "' where id='"+id+"'");
            Load_user();
        }
       
    }
    //xử lý check
    protected void loadgorup()
    {
        DataTable dtgroup = new DataTable();
        dtgroup = DataAccess.ExeSQLToDataTable("select * from admin_groupuser order by idgroup desc");
        RadioButtonList1.DataSource = dtgroup;
        RadioButtonList1.DataTextField = "namegroup";
        RadioButtonList1.DataValueField = "idgroup";
        RadioButtonList1.DataBind();
    }
    //protected void loadadmin_module_page()
    //{ string n="0";
    //    DataTable dtadmin_module_page = new DataTable();
    //    dtadmin_module_page = DataAccess.ExeSQLToDataTable("select * from admin_module_page where idcheck !='"+n+"'");
    //    CheckBoxList1.DataSource = dtadmin_module_page;
    //    CheckBoxList1.DataValueField = "idmodule";
    //    CheckBoxList1.DataTextField = "Name_Module";
    //    CheckBoxList1.DataBind();
    //}
  
    //protected void bt_click(object sender, EventArgs e)
    //{
    //    string nhomnguoidung = "";
    //    foreach (ListItem item1 in RadioButtonList1.Items)
    //    {

    //        if (item1.Selected)
    //        {
    //            // Response.Write("Bạn đã chọn quyền: " + item1.Value + "<br />");
    //            nhomnguoidung = item1.Value;
    //        }
    //    }
    //    foreach (ListItem item in CheckBoxList1.Items)
    //    {

    //        if (item.Selected)
    //        {
    //            Response.Write("Bạn đã chọn modul: " + nhomnguoidung + "<br />");
    //            Response.Write("Bạn đã chọn modul: " + item.Value + "<br />");
    //            DataAccess.ExeNonSQL("Insert into admin_roles(Idmodule,idgroup) values('" + item.Value + "','"+nhomnguoidung+"')");
    //        }
    //    }
    //}
}
